/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import Entidad.Empleado;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;

/**
 *
 * @author felipe
 */
public class EmpleadoDAO {
    
    private static EntityManagerFactory emf=Persistence.createEntityManagerFactory("CRMPU");

    public boolean crear(Empleado object) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean salida=false;
        try {
            em.persist(object);
            em.getTransaction().commit();
            salida = true;
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            em.close();
            return salida;
        }
    }
    
    public boolean eliminar(Empleado object) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Empleado com = em.find(Empleado.class, object.getID());
        boolean ret = false;
        try {
            em.remove(com);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }
    public Empleado leer(double par) {

        EntityManager em = emf.createEntityManager();
        Empleado empleado = em.find(Empleado.class, par);
        return empleado;
    }
    
    public boolean actualizar(Empleado object, Empleado nuevoObjeto) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean ret = false;
        try {
            object.setNombre(nuevoObjeto.getNombre());
            object.setID(nuevoObjeto.getID());
            object.setRol(nuevoObjeto.getRol());
            object.setContraseña(nuevoObjeto.getContraseña());
            
            em.merge(object);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
            }
        }
    public ArrayList<Double> listar (){
        ArrayList<Double> ids = new ArrayList<Double>();
        double l;
        EntityManager em = emf.createEntityManager();
        List lista;
        Query q = em.createQuery("SELECT c from Empleado c ");
        try {
            lista = q.getResultList();
            for (int i=0; i<lista.size(); i++){
                Empleado p = (Empleado)lista.get(i);
                //casting de double a int
                l = p.getID();
                ids.add(l);
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            return ids;
        }
    }
    
    public List<Empleado> bringAll(){
        EntityManager em = emf.createEntityManager();
        List<Empleado> cat = new ArrayList<Empleado>();
        
        Query q= em.createQuery("SELECT u FROM Empleado u");
        List<Empleado> tempCat = q.getResultList();
        try {
        for(Empleado c: tempCat){
            Empleado cate = new Empleado();
            cate.setNombre(c.getNombre());
            cate.setID(c.getID());
            cate.setRol(c.getRol());
            cate.setContraseña(c.getContraseña());
            cat.add(cate);
        }
        
        }
        catch(Exception e){
            e.printStackTrace();;            
        }
        finally {
            
            em.close();
            return cat;
        }
    }
}
